//
//  CAPWelcomeController.swift
//  CAPSwift
//
//  Created by captain on 16/4/20.
//  Copyright © 2016年 captain. All rights reserved.
//

import UIKit
import SDWebImage
class CAPWelcomeController: UIViewController {
    override func loadView() {
        view = bjImageView
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    //显示之后调用方法
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        setIconImageViewAnim()
    }
    
    // MARK: - 动画更新约束
    private func setIconImageViewAnim(){
        self.iconImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        
        //动画
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //重新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //动画
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    //显示label
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //发送通知
//                        print("发送通知")
                        NSNotificationCenter.defaultCenter().postNotificationName(NOTI, object: "welcome")
                })
        }
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        //添加控件
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)
        
        //添加约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSizeMake(85, 85))
            make.top.equalTo(view).offset(400)
            make.centerX.equalTo(view)
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
        }
        
    }
    
    // MARK: - 懒加载控件
    private lazy var bjImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var iconImageView: UIImageView = {
       let iconImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        iconImageView.layer.cornerRadius = 85 * 0.5
        iconImageView.clipsToBounds = true
        iconImageView.layer.borderColor = UIColor.orangeColor().CGColor
        iconImageView.layer.borderWidth = 2
        //下载头像
        
        iconImageView.sd_setImageWithURL(NSURL(string: CAPUserAccountTools.sharedTools.userAccountModel?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"
            ))
        

        return iconImageView
    }()
    //欢迎文字
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(),textFont: 17)
        label.textAlignment = NSTextAlignment.Center
        label.text = "欢迎回来"
        label.alpha = 0

        return label
        
    }()
    
    
}